package com.ts.proj.service.impl;

import com.ts.proj.entity.SupplierEntity;
import com.ts.proj.repository.SupplierRepository;
import com.ts.proj.service.SupplierService;

import com.ts.base.service.impl.CommonServiceImpl;

import com.ts.util.EntityAttributeUtil;
import com.ts.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


//说明 字符串：，作为替换字符串 LOWERSupplier                Supplier

@Service
public class SupplierServiceImpl extends CommonServiceImpl<SupplierEntity>  implements SupplierService {

    @Autowired
    private SupplierRepository supplierRepository;

    @Override
    public List<SupplierEntity> findAll(PageData pageData, Integer start, Integer length) {
        Specification<SupplierEntity> sp = this.getSpecification(pageData,SupplierEntity.class);
        Sort sort = new Sort(Sort.Direction.ASC,"id");
        Pageable pageable = new PageRequest(start, length, sort);
        return supplierRepository.findAll(sp,pageable).getContent();
    }

    @Override
    public long count(PageData pageData) {
        Specification<SupplierEntity> sp = this.getSpecification(pageData,SupplierEntity.class);
        return supplierRepository.count(sp);
    }

    @Override
    public boolean deleteByIds(Iterable<SupplierEntity> iterable) {
        supplierRepository.deleteInBatch(iterable);
        return false;
    }

    @Override
    public List<SupplierEntity> saveAll(List<SupplierEntity> entities) {
        return supplierRepository.saveAll(entities);
    }

    @Override
    public List<SupplierEntity> findAllById(Integer[] ids) {
        List<SupplierEntity> list = supplierRepository.findAllById(Arrays.asList(ids));
        if(list.size()==0){
            SupplierEntity entity = new SupplierEntity();
            list.add(entity);
        }
        return list;
    }

    @Override
    public boolean deleteAll() {
		supplierRepository.deleteAll();
        return false;
    }

    @Override
    public Specification getSpecification(final PageData pageData, final Class entityClass) {
        return new Specification() {
            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> preList = new ArrayList();
                List<String> fieldList = EntityAttributeUtil.entityAttribute(entityClass);

                for(int i = 0; i < fieldList.size(); ++i) {
                    if (null != pageData.get(fieldList.get(i))&&!fieldList.get(i).equals("supplierPY")) {
                        preList.add(criteriaBuilder.equal(root.get((String)fieldList.get(i)), pageData.get(fieldList.get(i)).toString()));
                    }
                    if(null != pageData.get(fieldList.get(i))&&fieldList.get(i).equals("supplierPY")){
                        preList.add(criteriaBuilder.like(root.get((String)fieldList.get(i)), "%"+pageData.get(fieldList.get(i)).toString()+"%"));
                    }

                }

                Predicate[] p = new Predicate[preList.size()];
                return criteriaBuilder.and((Predicate[])preList.toArray(p));
            }
        };
    }
}